package cn.hx.service.impl;

import cn.hx.controller.system.PageModel;
import cn.hx.dao.NewsDao;
import cn.hx.entity.News;
import cn.hx.service.NewsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 新闻(News)表服务实现类
 *
 * @author makejava
 * @since 2022-08-19 19:30:22
 */
@Service("newsService")
public class NewsServiceImpl implements NewsService {
    @Resource
    private NewsDao newsDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public News queryById(Integer id) {
        return this.newsDao.queryById(id);
    }

    /**
     * 个数
     *
     * @return 实例对象
     */
    @Override
    public int count() {
        return this.newsDao.count();
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<News> queryAllByLimit(News news, int offset, int limit) {
        return this.newsDao.queryAllByLimit(news, offset, limit);
    }
    @Override
    public List<News> queryAllByLimit1(News news, int offset, int limit) {
        return this.newsDao.queryAllByLimit1(news, offset, limit);
    }

    /**
     * 新增数据
     *
     * @return 实例对象
     */
    @Override
    public int insert(News news) {
        System.out.println(news);
        return this.newsDao.insert(news);
    }

    /**
     * 修改数据
     *
     * @return 实例对象
     */
    @Override
    public int update(News news) {
        return this.newsDao.update(news);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.newsDao.deleteById(id) > 0;
    }

    @Override
    public PageModel<News> page(int page, int size) {
        PageModel<News> instance = PageModel.getInstance(page, size);
        List<News> pets = newsDao.queryAllByLimit(new News(), (page - 1) * size, size);
        int count = newsDao.count();
        instance.setList(pets);
        instance.setCount(count);
        instance.setPageSize(count % size == 0 ? count / size : (count / size + 1));
        return instance;
    }

    @Override
    public int selectCount() {
        return newsDao.selectCount();
    }
}